<template>
  <view class="auth-container">
    <!-- 微信登录容器 -->
    <view class="auth-section wechat-section">
      <text class="section-title">微信服务号登录</text>
      <button class="auth-button" @click="handleWechatLogin">立即授权</button>
      <text class="section-tip">请在微信内打开页面</text>
    </view>

    <!-- 数据操作容器 -->
    <view class="auth-section data-section">
      <text class="section-title">数据管理</text>
      <button class="auth-button" @click="saveData">保存到Vuex</button>
      <text class="section-tip">仅用于功能演示</text>
    </view>
  </view>
</template>

<script>
export default {
	data() {
	    return {
	      wechatCode: null // 用于存储微信返回的code
	    };
	  },
  methods: {
    // 处理微信公众号登录
    handleWechatLogin() {        
	  const appId='wx0ebd7da2e72e6be2';
      //const redirectUri = encodeURIComponent(window.location.href); // 回调到当前页
	  const redirectUri = encodeURIComponent("https://ds.aixifs.com/ua/index.html#/pages/login/pageA"); // 回调到当前页
      const scope = 'snsapi_userinfo'; // 或 snsapi_base
      const state = 'fasfravfadfqwerbgwt';

      //const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
	  const authUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect`;
      window.location.href = authUrl;
    },

    // 在页面加载时检查URL中的code（微信回调后触发）
    onLoad(options) {
		console.log('onload options',options);
      const { code, state } = options;
      if (code) {
		  console.log('onload terms code',code);
		  this.wechatCode = code;
        this.saveData();
		// 调用原有login方法
        //this.login({ code }, 'weixin'); // 调用原有login方法
        //this.login({ code }, 'weixin'); // 调用原有login方法
      }
    },

    // 原有login方法（无需修改）
    login(params, type) {
	  console.log('login params',params);
	  let code = params['code']
      let action = 'loginBy' + type.trim().replace(type[0], type[0].toUpperCase());
      const uniIdCo = uniCloud.importObject("uni-id-co", { customUI: true });
      console.log('login code',code);
	 console.log('login action',action);
	 mutations.loginSuccess(code);
	 this.$store.dispatch('saveUserData', code); // 保存到Vuex
    //   uniIdCo[action](code)
    //     .then(result => {
    //       uni.showToast({ title: '登录成功', icon: 'none' });
		  // console.log('uniIdCo result',result);
    //       mutations.loginSuccess(result);
    //       this.$store.dispatch('saveUserData', result); // 保存到Vuex
    //     })
    //     .catch(e => {
    //       uni.showModal({ content: e.message, showCancel: false });
    //     });
    },
  
    saveData() {
     const userData = {
       name: '张三',
       age: 125,
       token: 'abc123',
	   code: this.wechatCode
     }
     // login(params, type)
     // 调用action保存数据
     this.$store.dispatch('saveUserData', userData)
     
     console.log('数据已保存到Vuex')
     this.$router.push('pageB') // 跳转到页面B
    }
  }
};
</script>
<style>
/* 主容器：垂直排列，居中 */
.auth-container {
  display: flex;
  flex-direction: column;
  gap: 30px;
  padding: 20px;
  align-items: center;
}

/* 每个功能区块的样式 */
.auth-section {
  width: 100%;
  max-width: 400px;
  border-radius: 12px;
  padding: 20px;
  background-color: #f8f8f8;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 微信登录区块特殊配色 */
.wechat-section {
  border-left: 4px solid #07C160; /* 微信绿色 */
}

/* 数据管理区块特殊配色 */
.data-section {
  border-left: 4px solid #2979FF; /* 蓝色 */
}

.section-title {
  display: block;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 15px;
}

.section-tip {
  display: block;
  font-size: 12px;
  color: #999;
  margin-top: 10px;
}

.auth-button {
  background-color: #fff;
  color: #333;
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 10px 0;
}
</style>